<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <!-- 搜索区域 -->
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="10" :sm="12">
            <a-form-item :label="$t('用户账号')" style="margin-left: 8px">
              <a-input :placeholder="$t('请输入账号')" v-model="queryParam.username"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search" style="margin-left: 18px">{{$t('查询')}}</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">{{$t('重置')}}</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 操作按钮区域 -->
    <div class="table-operator" :md="24" :sm="24" style="margin-top: -15px">
      <!--<a-button @click="handleEdit" type="primary" icon="edit" style="margin-top: 16px">用户编辑</a-button>-->
      <a-button @click="handleAddUserDepart" type="primary" icon="plus">{{ $t('添加已有用户') }}</a-button>
      <a-button @click="handleAdd" type="primary" icon="plus" style="margin-top: 16px">{{ $t('新建用户') }}</a-button>

      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete" />
            {{ $t('取消关联') }}
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          {{ $t('批量操作') }}
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i>
        <i18n path="selected-items" tag="span">
          <a place="items" style="font-weight: 600">{{ selectedRowKeys.length }}</a>
        </i18n>
        <a style="margin-left: 24px" @click="onClearSelected">{{ $t('清空') }}</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">{{ $t('编辑') }}</a>

          <a-divider type="vertical" />

          <a-dropdown>
            <a class="ant-dropdown-link"> {{ $t('更多') }} <a-icon type="down" /> </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a href="javascript:;" @click="handleDeptRole(record)">{{ $t('部门角色') }}</a>
              </a-menu-item>

              <a-menu-item>
                <a href="javascript:;" @click="handleDetail(record)">{{ $t('用户详情') }}</a>
              </a-menu-item>

              <a-menu-item>
                <a-popconfirm :title="$t('确定取消与选中部门关联吗?')" @confirm="() => handleDelete(record.id)">
                  <a>{{ $t('取消关联') }}</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <user-modal ref="modalForm" @ok="modalFormOk"></user-modal>
    <Select-User-Modal ref="selectUserModal" @selectFinished="selectOK"></Select-User-Modal>
    <dept-role-user-modal ref="deptRoleUser"></dept-role-user-modal>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getAction, postAction, deleteAction } from '@/api/manage'
import SelectUserModal from './SelectUserModal'
import UserModal from './UserModal'
import DeptRoleUserModal from './DeptRoleUserModal'

export default {
  name: 'DeptUserInfo',
  mixins: [JeecgListMixin],
  components: {
    DeptRoleUserModal,
    SelectUserModal,
    UserModal,
  },
  data() {
    return {
      description: this.$t('用户信息'),
      currentDeptId: '',
      currentDept: {},
      // 表头
      columns: [
        {
          title: this.$t('用户账号'),
          align: 'center',
          dataIndex: 'username',
        },
        {
          title: this.$t('用户名称'),
          align: 'center',
          dataIndex: 'realname',
        },
        {
          title: this.$t('部门'),
          align: 'center',
          dataIndex: 'orgCode',
        },
        {
          title: this.$t('性别'),
          align: 'center',
          dataIndex: 'sex_dictText',
        },
        {
          title: this.$t('电话'),
          align: 'center',
          dataIndex: 'phone',
        },
        {
          title: this.$t('操作'),
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
          align: 'center',
          width: 150,
        },
      ],
      url: {
        list: '/sys/user/departUserList',
        edit: '/sys/user/editSysDepartWithUser',
        delete: '/sys/user/deleteUserInDepart',
        deleteBatch: '/sys/user/deleteUserInDepartBatch',
      },
    }
  },
  created() {},

  methods: {
    searchReset() {
      this.queryParam = {}
      this.loadData(1)
    },
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error(this.$t('请设置url.list属性'))
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      //if (this.currentDeptId === '') return;
      let params = this.getQueryParams() //查询条件
      params.depId = this.currentDeptId
      getAction(this.url.list, params).then((res) => {
        if (res.success && res.result) {
          this.dataSource = res.result.records
          this.ipagination.total = res.result.total
        }
      })
    },
    batchDel: function () {
      if (!this.url.deleteBatch) {
        this.$message.error(this.$t('请设置url.deleteBatch属性'))
        return
      }
      if (!this.currentDeptId) {
        this.$message.error(this.$t('未选中任何部门，无法取消部门与用户的关联'))
        return
      }

      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning(this.$t('请选择一条记录'))
        return
      } else {
        var ids = ''
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ','
        }
        var that = this
        console.log(this.currentDeptId)
        this.$confirm({
          title: this.$t('确认取消'),
          content: this.$t('是否取消用户与选中部门的关联?'),
          okText: this.$t("确定"),
          cancelText: this.$t("取消"),
          onOk: function () {
            deleteAction(that.url.deleteBatch, { depId: that.currentDeptId, userIds: ids }).then((res) => {
              if (res.success) {
                that.$message.success(this.$t('删除用户与选中部门关系成功'))
                that.loadData()
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            })
          },
        })
      }
    },
    handleDelete: function (id) {
      if (!this.url.delete) {
        this.$message.error(this.$t('请设置url.delete属性'))
        return
      }
      if (!this.currentDeptId) {
        this.$message.error(this.$t('未选中任何部门，无法取消部门与用户的关联'))
        return
      }

      var that = this
      deleteAction(that.url.delete, { depId: that.currentDeptId, userId: id }).then((res) => {
        if (res.success) {
          that.$message.success(that.$t('删除用户与选中部门关系成功'))
          if (that.selectedRowKeys.length > 0) {
            for (let i = 0; i < that.selectedRowKeys.length; i++) {
              if (that.selectedRowKeys[i] == id) {
                that.selectedRowKeys.splice(i, 1)
                break
              }
            }
          }
          that.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    open(record) {
      //console.log(record);
      this.currentDeptId = record.id
      this.currentDept = record
      this.loadData(1)
    },
    clearList() {
      this.currentDeptId = ''
      this.dataSource = []
    },
    hasSelectDept() {
      if (this.currentDeptId == '') {
        this.$message.error(this.$t('请选择一个部门'))
        return false
      }
      return true
    },
    handleAddUserDepart() {
      if (this.currentDeptId == '') {
        this.$message.error(this.$t('请选择一个部门'))
      } else {
        this.$refs.selectUserModal.visible = true
      }
    },
    handleEdit: function (record) {
      this.$refs.modalForm.title = this.$t('编辑')
      this.$refs.modalForm.departDisabled = true
      this.$refs.modalForm.disableSubmit = false
      this.$refs.modalForm.edit(record)
    },
    handleAdd: function () {
      if (this.currentDeptId == '') {
        this.$message.error(this.$t('请选择一个部门'))
      } else {
        this.$refs.modalForm.departDisabled = true
        //初始化负责部门
        this.$refs.modalForm.nextDepartOptions = [{ value: this.currentDept.key, label: this.currentDept.title }]
        this.$refs.modalForm.title = this.$t('新增')
        this.$refs.modalForm.edit({ activitiSync: '1', userIdentity: 1, selecteddeparts: this.currentDeptId })
      }
    },
    selectOK(data) {
      let params = {}
      params.depId = this.currentDeptId
      params.userIdList = []
      for (var a = 0; a < data.length; a++) {
        params.userIdList.push(data[a])
      }
      console.log(params)
      postAction(this.url.edit, params).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          this.loadData()
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    handleDeptRole(record) {
      if (this.currentDeptId != '') {
        this.$refs.deptRoleUser.add(record, this.currentDeptId)
        this.$refs.deptRoleUser.title = this.$t('部门角色分配')
      } else {
        this.$message.warning(this.$t('请先选择一个部门'))
      }
    },
  },
}
</script>
<style scoped>
/** Button按钮间距 */
.ant-btn {
  margin-left: 3px;
}

.ant-card {
  margin-left: -30px;
  margin-right: -30px;
}

.table-page-search-wrapper {
  margin-top: -16px;
  margin-bottom: 16px;
}
</style>